<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\GoodsBrand;
use App\Services\GoodsBrandService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsBrandController extends AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'image' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method', '_token']);
        $id = $request->input('id');
        $flight = GoodsBrand::updateOrCreate(
            ['id' => $id],
            $putData
        );
        return $this->success($flight->id);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = GoodsBrandService::getInstance()->list($page,$limit,$sort,$order, $name);
        return $this->success($list);
    }
//删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goodsBrand = GoodsBrand::find($request->id);
        $goodsBrand->delete();
        return $this->success();
    }

    // 上传结果
    public function upfile(Request $request)
    {

        $url = config('filesystems.disks.goods.url');
        $default = config('filesystems.disks.goods.default');

        $file = GoodsBrandService::getInstance()->upfile($request, $url, $default, 'goods');
        return $this->success([
                'file' => $file,
            ]
        );
    }

}
